//
//  MCNewMarketingCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/26.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCNewMarketingCell_editClosure = (MCNewMarketingAddGoodsModel) -> Void
typealias MCNewMarketingCell_deleteClosure = (Int) -> Void

class MCNewMarketingCell: UITableViewCell {

    public var edit_closure : MCNewMarketingCell_editClosure?
    public var delete_closure : MCNewMarketingCell_deleteClosure?

    var model = MCNewMarketingAddGoodsModel() {
        didSet {
            iconImageView.sd_setImage(with: URL.init(string: model.navigateImg1), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            nameLabel.text = model.goodsName
            
            moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.lowestPrice)
            suggestLabel.text = "市场价:" + MCString_judgemantDecimalsIsZero(figure: model.suggestedPrice.intValue)
            inventoryLabel.text = "库存:" + model.inventoryNum.stringValue
        }
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
   
        backgroundColor = UIColor.MCGray_light
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, 0, 1, 0))
        }
        
        bgView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgView).offset(15)
            make.centerY.equalTo(bgView)
            make.size.equalTo(CGSize.init(width: 80, height: 80))
        }
        
        
        bgView.addSubview(inventoryLabel)
        inventoryLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.bottom.equalTo(iconImageView)
        }
        
        bgView.addSubview(editButton)
        editButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(bgView).offset(-15)
            make.centerY.equalTo(inventoryLabel)
            make.size.equalTo(CGSize.init(width: 60, height: 25))
        }
        
        bgView.addSubview(deleteButton)
        deleteButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(editButton.snp.left).offset(-10)
            make.centerY.equalTo(inventoryLabel)
            make.size.equalTo(CGSize.init(width: 60, height: 25))
        }
        
        
        bgView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(inventoryLabel)
            make.bottom.equalTo(inventoryLabel.snp.top).offset(-5)
        }

        
        bgView.addSubview(suggestLabel)
        suggestLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(moneyLabel.snp.right).offset(5)
            make.bottom.equalTo(moneyLabel)
        }

        
        bgView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.right.equalTo(bgView).offset(-15)
            make.top.equalTo(iconImageView)
            make.bottom.lessThanOrEqualTo(moneyLabel.snp.top).offset(-5)
        }

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func editButtonClicked() {
        edit_closure?(model)
    }
    
    @objc func deleteButtonClicked() {
        delete_closure?(model.goodsId.intValue)
    }
    
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.borderColor = UIColor.MCGray_light.cgColor
        imageView.layer.borderWidth = 1
        return imageView
    }()
    
    
    public lazy var nameLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(15)
        label.textColor = UIColor.MCBlack
        label.numberOfLines = 0
        return label
    }()
    
    
    lazy var editButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        let image = #imageLiteral(resourceName: "Reuse_share_black").MCImageWithTintColor(color: UIColor.MCGray_middle)
        button.setImage(image, for: UIControlState.normal)
        button.titleLabel?.font = MCFont(12)
        button.setTitle(" 编辑", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCWords, for: UIControlState.normal)
        
        
        button.imageEdgeInsets = UIEdgeInsetsMake(6, 10, 6, 37)
        
        button.addTarget(self, action: #selector(editButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var deleteButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        let image = #imageLiteral(resourceName: "Reuse_delete")
        button.setImage(image, for: UIControlState.normal)
        button.titleLabel?.font = MCFont(12)
        button.setTitle(" 删除", for: UIControlState.normal)
        button.setTitleColor(UIColor.MCWords, for: UIControlState.normal)
        
        
        button.imageEdgeInsets = UIEdgeInsetsMake(5.5, 10, 6.5, 37)

        button.addTarget(self, action: #selector(deleteButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()

    
    
    public lazy var moneyLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(13)
        label.textColor = UIColor.MCMain
        return label
    }()
    
    public lazy var suggestLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()

    
    public lazy var inventoryLabel: UILabel = {
        let label = UILabel.init()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
}
